package com.syedu.day02;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @Description JDK8.0提供的全新日期, 用来代替Date和Calendar
 * @createTime 2022/3/4 14:12
 * @author Administrator
 */
public class LocalDateDemo {
    public static void main(String[] args) {
        //当前时间
        //年月日
        LocalDate date = LocalDate.now();
        System.out.println(date);

        //时分秒
        LocalTime time = LocalTime.now();
        System.out.println(time);

        //年月日+时分秒
        LocalDateTime dateTime = LocalDateTime.now();
        //格式化时间
        String formatDate = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(formatDate);

        //把字符串转换成日期对象
        //注意: 如果模板符合yyyy-MM-dd HH:mm:ss,则可以直接转换,否则运行错误,当然我们也可以自己指定转换的模板
        String str = "2000/02/05";
        //LocalDate birthday = LocalDate.parse(str);
        LocalDate birthday = LocalDate.parse(str, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        System.out.println(birthday);

        //获取其他时间
        LocalDateTime newDate = LocalDateTime.of(2002, 12, 12, 12, 12, 12);
        System.out.println(newDate);

        //获取时间信息
        int year = dateTime.getYear();
        //Month month = dateTime.getMonth();
        int month = dateTime.getMonthValue();
        System.out.println(month);

        //DayOfWeek dayOfWeek = dateTime.getDayOfWeek();
        int week = dateTime.getDayOfWeek().getValue();
        System.out.println(week);

        System.out.println("--------------------------------------------------------------");

        /**
         * 新版日期区别于老版日期的特性
         * 日期换算:
         * 老版日期需要转换成毫秒数,然后进行计算,新版日期可以直接在日期的上面进行运算
         */
        //LocalDateTime plus = dateTime.plusDays(10);//加10天
        //LocalDateTime plus = dateTime.plusDays(-10);//减10天
        //LocalDateTime plus = dateTime.minusDays(10);//减10天

        //加指定单位的时间
        LocalDateTime plus = dateTime.plus(10, ChronoUnit.YEARS);
        System.out.println(plus);

        System.out.println("--------------------------------------------------------------");

        //判断日期先后
        boolean before = plus.isBefore(dateTime);
        System.out.println(before);

        boolean after = plus.isAfter(dateTime);
        System.out.println(after);
    }
}
